package com.test.uiapp2.function.Compatible;

import android.util.Log;

import androidx.test.uiautomator.UiObject;

import com.test.uiapp2.base.TestBase;
import com.test.uiapp2.element.Base.BaseElement;
import com.test.uiapp2.element.Compatible.CompatibleElement;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class CompatibleFunction extends TestBase {

    public void AppCompatible(){
        startApp(CompatibleElement.YYB);
        sleep(WAIT);
        try {
            if(getId(BaseElement.DOWNLOADER,CompatibleElement.AWT).exists()){
                //爬虫应用宝数据
                Document document = Jsoup.connect("https://sj.qq.com/myapp/category.htm?orgame=1").get();
                Elements select = document.select("div.app-info-desc");
                Log.i(TAG, "应用的总数="+select.size()+"个");
                for (int i=0;i<select.size();i++){
                    if(i != 0){
                        sleep(SHOTWAIT);
                        startApp(CompatibleElement.YYB);
                        sleep(WAIT);
                    }
                    clickById(BaseElement.DOWNLOADER,CompatibleElement.AWT);
                    String appname = document.select("div.app-info-desc").select("a.com-install-btn").get(i).attr("appname");
                    String PackagesName = document.select("div.app-info-desc").select("a.com-install-btn").get(i).attr("apk");
                    getId(BaseElement.DOWNLOADER,CompatibleElement.YV).setText(appname);
                    sleep(WAIT);
                    clickById(BaseElement.DOWNLOADER,CompatibleElement.A5T);
                    sleep(SHOTWAIT);
                    oneclick();
                    sleep(SHOTWAIT);
                    InstallApp();
                    StartApp();
                    verify("应用不兼容:"+appname,!getTest(CompatibleElement.NORESPONSE2).exists());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //安装
    public void InstallApp(){
        //应用详情界面，点击下载
        if(getId(BaseElement.DOWNLOADER,CompatibleElement.WT).exists()){
            clickById(BaseElement.DOWNLOADER,CompatibleElement.WT);
        }
        Log.i(TAG, "InstallApp: 监听是否下载完成");
        for(int i=1;i<20;i++){
            sleep(MWAITTIME);
            Log.i(TAG, "InstallApp: 检测是否下载完成第"+i+"次");
            if(getTest(CompatibleElement.ALERTINSTALLAPP).exists()){
                Log.i(TAG, "已下载完成");
                sleep(WAITTIME);
                clickByText(CompatibleElement.INSTALLAPP);
                break;
            }else{
                Log.i(TAG, "未下载完成");
            }
        }
    }
    //安装
    public void StartApp(){
        Log.i(TAG, "InstallApp: 监听是否安装完成");
        for(int i=1;i<20;i++){
            sleep(MWAITTIME);
            Log.i(TAG, "InstallApp: 检测是否安装完成第"+i+"次");
            if(getTest(CompatibleElement.ALERTSTARTAPP).exists()){
                Log.i(TAG, "已安装完成");
                sleep(WAIT);
                clickByText(CompatibleElement.STARTAPP);
                sleep(WAITTIME);
                break;
            }else{
                Log.i(TAG, "未安装完成");
            }
        }
    }

    //列表点击第一个
    public  void oneclick(){
        try {
            sleep(WAITTIME);
            uiDevice.click(732,281);
            sleep(WAIT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public  void AppCompatibleSnappea(String appname){
        try {
//                Document document = Jsoup.connect("https://www.wandoujia.com/top/app").get();
//                Elements detail = document.select("a.detail-check-btn");
//                for (int i=0;i<detail.size();i++) {
//                  String appname = detail.get(i).attr("data-app-name");

                    startApp(CompatibleElement.SNAPPEA);
                    sleep(WAITTIME);
                    if(getTextContains("版本新特性").exists()){
                        Log.i(TAG, "版本升级提示：版本新特性");
                        clickByText(CompatibleElement.QUXIAO);
                        sleep(WAIT);
                    }
                    UiObject sc = getId(BaseElement.SNAPPEA, CompatibleElement.SERACH);
                    UiObject xsc = getId(BaseElement.SNAPPEA,CompatibleElement.EO);
                    UiObject hl = getId(BaseElement.SNAPPEA,CompatibleElement.HL);
                    if(sc.exists()){
                        clickById(BaseElement.SNAPPEA,CompatibleElement.SERACH);
                    }else if(xsc.exists()){
                        clickById(BaseElement.SNAPPEA,CompatibleElement.EO);
                    }else if(hl.exists()){
                        clickById(BaseElement.SNAPPEA,CompatibleElement.HL);
                    }else{
                        verify("界面异常",sc.exists() || xsc.exists());
                    }
                    sleep(HWAIT);
                    getId(BaseElement.SNAPPEA,CompatibleElement.SERACHSEND).setText(appname);
                    sleep(HWAIT);
                    clickById(BaseElement.SNAPPEA,CompatibleElement.HL);
                    sleep(WAITTIME*2);
                    uiDevice.click(654,242);
                    AppInstall();
//                  uiDevice.executeShellCommand("am force-stop com.wandoujia.phoenix2");
                    //断言
                    verify("应用不兼容:",result());
                    sleep(WAITTIME);
                    minimize();
                    sleep(HWAIT);
                    uiDevice.swipe(243,1172,982,1199,STEP);
                    sleep(HWAIT);
                    uiDevice.swipe(1045,1205,258,1195,STEP);


//                }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //判断启动应用是否出现未响应
    public  boolean result( ){
        Log.i(TAG, "判断当前应用是否未响应");
        sleep(WAITTIME);
        try {
            if(getTextContains(CompatibleElement.NORESPONSE).exists() || getTextContains(CompatibleElement.NORESPONSE1).exists() || getTextContains(CompatibleElement.NORESPONSE2).exists()){
                return  false;
            }else{
                Log.i(TAG, "应用启动正常");
                return  true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  true;
    }

    public  void AppInstall(){
        sleep(WAITTIME*5);
        if(getTest(CompatibleElement.INSTALLAPP).exists() ||getTest(CompatibleElement.UPGRADE).exists()){
            if(getTest(CompatibleElement.INSTALLAPP).exists()){
                clickByText(CompatibleElement.INSTALLAPP);
            }else{
                clickByText(CompatibleElement.UPGRADE);
            }
            sleep(MWAITTIME*2);
            int j=1;
            while (getTest(CompatibleElement.STOP).exists() && !getTest(CompatibleElement.WEIZHI).exists()){
                Log.i(TAG, "监听是否下载完成第"+j+"次");
                sleep(WAIT);
                j++;
            }
            sleep(MWAITTIME);
            clickByText(CompatibleElement.INSTALLAPP);
            AppStart();
        }else if(getTest(CompatibleElement.STARTAPP).exists()){
            clickByText(CompatibleElement.STARTAPP);
        }else{
            Log.i(TAG, "AppInstall: 未进入安装界面");
            verify("未进入安装界面",getTest(CompatibleElement.INSTALLAPP).exists()||getTest(CompatibleElement.UPGRADE).exists());
        }
    }
    public  void AppStart(){
        sleep(WAITTIME);
        int n=1;
        while (getTest(CompatibleElement.Install).exists()){
            Log.i(TAG, "监听是否安装完成第"+n+"次");
            sleep(WAIT);
            n++;
        }
        sleep(WAITTIME);
        if(getId(CompatibleElement.INSTALLSUCCESS).exists()){
            Log.i(TAG, "已安装完成");
            clickByText(CompatibleElement.STARTAPP);
            sleep(WAIT);
        }
    }

    public void JumpOver(){
        if(getTest(CompatibleElement.JUMPOVER).exists()){
            clickByText(CompatibleElement.JUMPOVER);
            sleep(WAIT);
        }else{
            Log.i(TAG, "JumpOver: 不存在广告界面");
        }
    }





}
